<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item v-if="isAuth('resume:resume:search')">
        <el-input v-model="dataForm.name" placeholder="姓名" clearable></el-input>
      </el-form-item>
      <el-form-item v-if="isAuth('resume:resume:search')">
        <el-input v-model="dataForm.major" placeholder="专业" clearable></el-input>
      </el-form-item>
      <el-form-item v-if="isAuth('resume:resume:search')">
        <el-select v-model="dataForm.education" placeholder="请选择学历">
          <el-option v-for="item in educationList" :key="item.id" :value="item.education" :label="item.education">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="isAuth('resume:resume:search')">
        <el-popover
          ref="menuListPopover"
          placement="bottom-start"
          trigger="click">
          <el-tree
            :data="menuList"
            :props="menuListTreeProps"
            node-key="menuId"
            ref="menuListTree"
            @current-change="menuListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
        </el-popover>
        <el-input v-model="dataForm.departmentName" v-popover:menuListPopover :readonly="true" placeholder="点击选择部门"
                  class="menu-list__input"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()" v-if="isAuth('resume:resume:search')">查询</el-button>
        <el-button @click="reset()" v-if="isAuth('resume:resume:search')">重置</el-button>
        <el-button v-if="isAuth('resume:resume:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('resume:resume:delete')" type="danger" @click="deleteHandle()"
                   :disabled="dataListSelections.length <= 0">批量删除
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="id">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="姓名">
      </el-table-column>
      <el-table-column
        prop="sex"
        header-align="center"
        align="center"
        label="性别">
      </el-table-column>
      <el-table-column
        prop="age"
        header-align="center"
        align="center"
        label="年龄">
      </el-table-column>
      <el-table-column
        prop="workYears"
        header-align="center"
        align="center"
        label="工作年限">
      </el-table-column>
      <el-table-column
        prop="education"
        header-align="center"
        align="center"
        label="学历">
      </el-table-column>
      <el-table-column
        prop="major"
        header-align="center"
        align="center"
        label="专业">
      </el-table-column>
      <el-table-column
        prop="nativePlace"
        header-align="center"
        align="center"
        label="籍贯">
      </el-table-column>
      <el-table-column
        prop="phone"
        header-align="center"
        align="center"
        label="联系方式">
      </el-table-column>
      <!--      <el-table-column-->
      <!--        prop="skill"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        label="教育背景">-->
      <!--      </el-table-column>-->
      <el-table-column
        prop="departmentName"
        header-align="center"
        align="center"
        label="部门名称">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="源文件">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="download(scope.row.fileUrl)">下载</el-button>
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="状态">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="showDetail(scope.row.id)">查看详情</el-button>
          <el-button type="text" size="small" v-if="isAuth('resume:resume:update')"
                     @click="addOrUpdateHandle(scope.row.id)">修改
          </el-button>
          <el-button type="text" size="small" v-if="isAuth('resume:resume:delete')" @click="deleteHandle(scope.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './resume-add-or-update'
import {treeDataTranslate} from '@/utils'

export default {
  data() {
    return {
      dataForm: {
        name: '',
        major: '',
        education: '',//学历
        departmentName: '',//部门名
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      educationList: [],
      menuList: [],
      menuListTreeProps: {
        label: 'name',
        children: 'children'
      },
      role: {},
    }
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList()
    this.geteducationList()
    this.getdepartmentList()
  },
  methods: {
    reset() {
      this.dataForm = {
        name: '',
        major: '',
        education: '',//学历
        departmentName: '',//部门名
      }
      this.getDataList()
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      /* name: '',
        major: '',
        education: '',//学历
        departmentId: '',//部门id*/
      this.$http({
        url: this.$http.adornUrl('/resume/resume/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'name': this.dataForm.name,
          'major': this.dataForm.major,
          'education': this.dataForm.education,
          'departmentName': this.dataForm.departmentName,
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 获取学历列表
    geteducationList() {
      this.$http({
        url: this.$http.adornUrl('/resume/education/all'),
        method: 'get',
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.educationList = data.data
        } else {
          this.educationList = []
        }
      })
    },
    //获取部门信息
    getdepartmentList() {
      this.$http({
        url: this.$http.adornUrl('/resume/department/list'),
        method: 'get',
        params: this.$http.adornParams({"key": ""})
      }).then(({data}) => {
        console.log('datadata', data);
        this.menuList = treeDataTranslate(data.page, 'menuId')
      })
    },
    // 菜单树选中
    menuListTreeCurrentChangeHandle(data, node) {
      this.dataForm.departmentId = data.menuId
      this.dataForm.departmentName = data.name
      // this.getDataList()
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    download(url) {
      console.log(url);
      window.open(window.SITE_CONFIG['staticDomainURL'] + url)
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 详情
    showDetail(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
        this.$refs.addOrUpdate.showDetail(true)
      })
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/resume/resume/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    }
  }
}
</script>
